<?php

class Termination_model extends MY_Model {

    function __construct() {
        parent::__construct();
        $this->table = 'termination_letter';
    }
    function getSearch($params, $sort = 'id', $order = 'asc') {
    	$this->db->select('termination_letter.*,member.first_name, member.last_name, product.product_name');
    	if (isset($params['full_name']) && $params['full_name'] != '') {
    		//$this->db->like('member.first_name', trim($params['first_name']), 'both');
    		$this->db->where("(member.first_name LIKE '%".$params['full_name']."%' OR last_name LIKE '%".$params['full_name']."%' OR email LIKE '%".$params['full_name']."%')");
    	}
    	if (isset($params['gender_value']) && $params['gender_value'] != '') {
    		$this->db->where('gender', $params['gender_value']);
    	}
    	if (isset($params['owner']) && $params['owner'] != '') {
    		$this->db->where('member.owner', 0);
    	}
    	if(isset($params['owner_id']) && $params['owner_id'] == -1){
    		$this->db->where('member.owner', 0);
    	}else if (isset($params['owner_id']) && $params['owner_id'] != '') {
    		$this->db->where('member.owner', $params['owner_id']);
    	}
    	$this->db->join("member","termination_letter.member_id = member.id");
    	$this->db->join("product","termination_letter.product_id = product.id");
    	$this->db->order_by('member.' . $sort, $order);
    	$this->db->order_by('member.update_date', 'desc');
    	$query = $this->db->get($this->table);
    	return $query->result();
    }
    
    function getForEmail( $id ) {
    	$this->db->select('termination_letter.id,member.gender,member.insertion,member.first_name,member.last_name,member.address,
    			member.house_number,member.postcode,member.place,member.email,member.phone,member.birthday,member.death,
    			termination_letter.contract_number,product.product_name,provider.provider_name,category.category_name,category.content_letter');
    	$this->db->where('termination_letter.id', $id);
    	$this->db->join("member","termination_letter.member_id = member.id");
    	$this->db->join("product","termination_letter.product_id = product.id");
    	$this->db->join("provider","termination_letter.provider_id = provider.id");
    	$this->db->join("category","termination_letter.category_id = category.id");
    	$this->db->order_by('member.update_date', 'desc');
    	$query = $this->db->get($this->table);
    	//print_r( $this->db->last_query() );
    	return $query->result();
    }
}
?>